package jjn.round1;

/**
 * @author Jiang Jining
 * @since 2023-04-28 23:33
 */
public class LeetCode64_MinimumPathSum {
    public int minPathSum(int[][] grid) {
        int height = grid.length;
        int width = grid[0].length;
        int[][] sum = new int[height][width];
        sum[0][0] = grid[0][0];
        for (int i = 1; i < height; i++) {
            sum[i][0] = grid[i][0] + sum[i - 1][0];
        }
        for (int i = 1; i < width; i++) {
            sum[0][i] = grid[0][i] + sum[0][i - 1];
        }
        for (int i = 1; i < height; i++) {
            for (int j = 1; j < width; j++) {
                sum[i][j] = grid[i][j] + Math.min(sum[i - 1][j], sum[i][j - 1]);
            }
        }
        return sum[height - 1][width - 1];
    }
}
